package com.example.zhinongbao.adapter;


import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;

import com.example.zhinongbao.R;



import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.example.zhinongbao.model.Order;
import java.util.ArrayList;
import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.OrderViewHolder> {

    private List<Order> orderList = new ArrayList<>();

    @NonNull
    @Override
    public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 加载订单项布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_order, parent, false);
        return new OrderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull OrderViewHolder holder, int position) {
        // 获取当前订单数据
        Order order = orderList.get(position);

        // 绑定数据到视图
        holder.bind(order);
    }

    @Override
    public int getItemCount() {
        return orderList.size();
    }

    // 修复点：添加updateOrders方法
    public void updateOrders(List<Order> newOrders) {
        this.orderList = newOrders != null ? newOrders : new ArrayList<>();
        notifyDataSetChanged(); // 通知适配器数据已变更
    }

    // ViewHolder内部类
    public static class OrderViewHolder extends RecyclerView.ViewHolder {
        private final TextView orderIdTextView;
        private final TextView productNameTextView;
        private final TextView statusTextView;
        private final TextView amountTextView;
        private final TextView dateTextView;

        public OrderViewHolder(@NonNull View itemView) {
            super(itemView);
            // 初始化视图组件
            orderIdTextView = itemView.findViewById(R.id.tv_order_id);
            productNameTextView = itemView.findViewById(R.id.tv_product_name);
            statusTextView = itemView.findViewById(R.id.tv_order_status);
            amountTextView = itemView.findViewById(R.id.tv_order_amount);
            dateTextView = itemView.findViewById(R.id.tv_order_date);
        }

        // 绑定数据方法
        public void bind(Order order) {
            orderIdTextView.setText("订单号: " + order.getOrderId());
            productNameTextView.setText(order.getProductName());
            statusTextView.setText(order.getStatus());
            amountTextView.setText(String.format("¥%.2f", order.getTotalAmount()));
            dateTextView.setText(order.getOrderDate());
        }
    }
}